<?php
include('./include/conn.php');
$u = htmlspecialchars(trim($_POST['u']));
$p = htmlspecialchars(trim($_POST['p']));
$rp = htmlspecialchars(trim($_POST['rp']));

$query = "SELECT `password` FROM `users` WHERE name ='".$u."'";
//echo $query
$rs = $link->query($query);
$row = $rs->fetch_assoc();
//var_dump($row);
if($row['password']){
	echo "<script>alert('用户名已经存在，请重新注册新账号！'); window.history.go(-1);</script>";
}else{
	//判断两次密码是否一致
	if($p == $rp){
		//默认等级
		$level = 0;
		//默认身份 2：普通用户
		$status = 2;

		$query = "INSERT INTO `users` VALUES(null,'".$u."','".$p."','".$level."','".$status."')";
		//echo $query;exit;

		if($link->query($query)){
			echo "<script>alert('用户注册成功，请登录！'); window.location.href='login.php';</script>";
		
		}else{
			echo "<script>alert('操作失败！'); window.history.go(-1);</script>";
		}
	}else{
		echo "<script>alert('两次密码输入不一致！'); window.history.go(-1);</script>";
	}
}
?>